package cn.tedu.sentinel03.controller;



import cn.tedu.sentinel03.service.HelloService;
import cn.tedu.sentinel03.until.Block;
import cn.tedu.sentinel03.until.Fallback;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class HelloController {
    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    //blockHandlerClass指定自定义的降级类
    //blockHandler = "blockFunc",指定的是限流方法名
    //指定降级类fallbackClass
    //指定降级方法，fallback = "fallbackFunc"
    @SentinelResource(value = "sayHi"
            ,blockHandlerClass = Block.class,blockHandler = "blockFunc"
            ,fallbackClass = Fallback.class,fallback = "fallbackFunc")
    public String sayHi(String name)  {
            String result = helloService.sayHi(name);
        if (true){
                System.out.println("模拟请求处理失败");
                throw new RuntimeException();
            }
//        try {
//            Thread.sleep(500);
//            if (true){
//                System.out.println("模拟请求处理失败");
//                throw new RuntimeException();
//            }
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        //模拟抛出异常，方便测试降级效果
            return result;
    }



}
